<template>
	<view class="container">
		<view class="scan" @click="scan">
			<image src="/static/img/agent/scan-code.png" mode="widthFix"></image>
			<text>扫一扫</text>
		</view>
	</view>
</template>

<script>
const jweixin = require('../../jweixin/jweixin');
import { scanqr, opendoor } from '../../api/api.js';
export default {
	data() {
		return {
			// key: value
			code: ''
		};
	},
	async onLoad() {
		await this.getConfig();
	},
	methods: {
		async getConfig() {
			let url = window.location.href;
			await scanqr({
				url,
				type: 4
			}).then(res => {
				let s = res.data;
				jweixin.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: s.appId, // 必填，公众号的唯一标识
					timestamp: s.timestamp, // 必填，生成签名的时间戳
					nonceStr: s.nonceStr, // 必填，生成签名的随机串
					signature: s.signature, // 必填，签名
					jsApiList: s.jsApiList // 必填，需要使用的JS接口列表
				});
			});
		},
		// scan() {
		// 	let that = this
		// 	var obj = {}
		// 	jweixin.scanQRCode({
		// 		needResult: 1,
		// 		scanType: ["qrCode"],
		// 		success: function(res) {
		// 			let code = res.resultStr
		// 			if (code.indexOf('?') != -1) {
		// 				let index = code.indexOf('?')
		// 				code = code.substring(index + 1, code.length)
		// 				code = code.split("&")
		// 				code.forEach(v => {
		// 					let arr = v.split('=')
		// 					obj[arr[0]] = arr[1]
		// 				})
		// 				that.code = obj.code
		// 				uni.navigateTo({
		// 					url: '/pages/agent/binding/binding?code=' + that.code
		// 				})
		// 			}
		// 		}
		// 	})
		// },
		scan() {
			// uni.navigateTo({
			// 	url: '/pages/agent/state?code=888888'
			// });
			// return;

			let that = this;
			var obj = {};
			jweixin.scanQRCode({
				needResult: 1,
				scanType: ['qrCode'],
				success: function(res) {
					let code = res.resultStr;
					if (code.indexOf('?') != -1) {
						let index = code.indexOf('?');
						code = code.substring(index + 1, code.length);
						code = code.split('&');
						code.forEach(v => {
							let arr = v.split('=');
							obj[arr[0]] = arr[1];
						});
						that.code = obj.code;
						// 到电池列表页-state
						uni.navigateTo({
							url: `/pages/agent/state?code=${that.code}`
						});
						// 打开柜门
						// opendoor({
						// 	code: that.code
						// }).then(res => {
						// 	uni.showModal({
						// 		title: '提示',
						// 		content: res.data.item_code + '柜门已打开',
						// 		showCancel: false,
						// 		success: function(res) {}
						// 	});
						// });
					}
				}
			});
		}
	}
};
</script>

<style scoped lang="scss">
.container {
	width: 100%;
	height: 100%;
	background-color: #f6f6f6;
	display: flex;
	align-items: center;
	justify-content: center;

	.scan {
		width: 460upx;
		height: 460upx;
		border-radius: 50%;
		background-image: url(../../static/img/agent/scan-bgc.png);
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #ffffff;

		image {
			width: 200upx;
		}

		text {
			margin-top: 10upx;
			font-size: 36upx;
		}
	}
}
</style>
